package com.labor_management_system.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.labor_management_system.Entity.Project;
import com.labor_management_system.Service.ProjectService;
import com.labor_management_system.Mapper.ProjectMapper;
import com.labor_management_system.Utils.Pair;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author deathwing475
* @description 针对表【project】的数据库操作Service实现
* @createDate 2024-02-27 09:54:53
*/
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
    implements ProjectService {
    //项目



    @Override
    public Boolean insertProject(Project project) {
        int row =0;

        row = baseMapper.insert(project);

        if(row!=0){
            return true;
        }

        return false;

    }


    @Override
    public List<Pair<String, String>> getProjectIDAndName() {
        List<Project> t = baseMapper.selectList(null);
        List<Pair<String, String>> backlist= new ArrayList<>();
        for(Project p : t){
            Pair<String, String> s = new Pair<>(p.getProjectId(),p.getProjectName());
            backlist.add(s);
        }
        return backlist;
    }
    @Override
    public List<Project> showAllProject(Integer selectPage, Integer pageSize) {
        Page<Project> Page = new Page<>(selectPage, pageSize);
        baseMapper.selectPage(Page,null);
        return Page.getRecords();
    }

    @Override
    public List<Project> selectBy(Project project) {
        QueryWrapper<Project> queryWrapper =new QueryWrapper<>();

        if(project.getProjectName()!=null){
            queryWrapper.eq("project_name",project.getProjectName());
        }

        else if(project.getProjectPhase()!=null){
            queryWrapper.eq("project_phase",project.getProjectPhase());
        }

        else if(project.getOwner()!=null){
            queryWrapper.eq("owner",project.getOwner());
        }

        else if(project.getDispatchWorkers()!=null){
            queryWrapper.eq("dispatch_workers",project.getDispatchWorkers());
        }

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Project selectProjectById(String projectId) {

        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_id",projectId);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean updateProject(Project project,String projectId) {
        int row =0;

        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_id",projectId);

        row = baseMapper.update(project,queryWrapper);

        if(row!=0){
            return true;
        }

        return false;
    }
    @Override
    public List<Project> selectAllProject() {
        return baseMapper.selectList(null);
    }


//    @Override
//    public void deleteproject(String projectId) {
//
//        //查询主表的数据
//        Project project = projectMapper.selectById(projectId);
//
//        //如果主表数据存在
//        if(project !=null){
//            //查询关联表的外键数据
//            List<LaborEngineering> laborEngineeringList = laborEngineeringMapper.selectList(new QueryWrapper<LaborEngineering>().eq("project_id", projectId));
//
//            //逐一处理关联表的数据
//            for(LaborEngineering laborEngineering:laborEngineeringList){
//                List<WorkDetails> workDetailsList = workDetailsMapper.selectList(new QueryWrapper<WorkDetails>().eq("labor_engineering_id", laborEngineering.getLaborEngineeringId()));
//
//                //逐一删除第三张表的的外键数据
//                for(WorkDetails workDetails:workDetailsList){
//                    workDetailsMapper.deleteById(workDetails.getWorkDetailsId());
//                }
//
//                // 最后删除关联表的数据
//                laborEngineeringMapper.deleteById(laborEngineering.getLaborEngineeringId());
//            }
//
//            //删除子表
//            QueryWrapper<ProjectList> listQueryWrapper = new QueryWrapper<>();
//            listQueryWrapper.eq("project_id",projectId);
//            projectListMapper.delete(listQueryWrapper);
//
//            // 最后删除主表的数据
//            projectMapper.deleteById(projectId);
//        }
    //项目
}




